package com.yanqu.road.dao.impl.activity.gourdchild;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.gourdchild.GourdChildPassDetail;
import com.yanqu.road.entity.activity.gourdchild.GourdChildUserData;
import com.yanqu.road.entity.activity.gourdchild.Grid;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GourdChildUserDataDaoImpl extends TempDataObjectDao<GourdChildUserData> {


    public static String getGourdChildUserDataSql = "select * from `t_u_activity_gourd_child_user_data` where `activityId`=? and `userId`=?";
    public GourdChildUserData getGourdChildUserData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getGourdChildUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String insertGourdChildUserDataSql = "insert into `t_u_activity_gourd_child_user_data`(activityId,userId,score,energy,lastRecTime,passId,gridList,nextGrid,passStar,lotusLv,trainDamage,normalDamage,passDetail,totalTimes,commonTimes,drawBoxList,getSharedBox,historyPassId) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(GourdChildUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastRecTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPassId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getGridList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getNextGrid())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getPassStar())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLotusLv()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTrainDamage()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getNormalDamage()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getPassDetail())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTotalTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCommonTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getDrawBoxList())));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGetSharedBox()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getHistoryPassId()));
        if (getDb().executeNoneQuery(insertGourdChildUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateGourdChildUserDataSql = "update `t_u_activity_gourd_child_user_data` set score=?,energy=?,lastRecTime=?,passId=?,gridList=?,nextGrid=?,passStar=?,lotusLv=?,trainDamage=?,normalDamage=?,passDetail=?,totalTimes=?,commonTimes=?,drawBoxList=?,getSharedBox=?,historyPassId=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(GourdChildUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastRecTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPassId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getGridList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getNextGrid())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getPassStar())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLotusLv()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTrainDamage()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getNormalDamage()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getPassDetail())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTotalTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCommonTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getDrawBoxList())));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGetSharedBox()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getHistoryPassId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateGourdChildUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected GourdChildUserData parseData(ResultSet rs) throws SQLException {
        GourdChildUserData gourdChildUserData = new GourdChildUserData();
        gourdChildUserData.setActivityId(rs.getInt("activityId"));
        gourdChildUserData.setUserId(rs.getLong("userId"));
        gourdChildUserData.setScore(rs.getLong("score"));
        gourdChildUserData.setEnergy(rs.getInt("energy"));
        gourdChildUserData.setLastRecTime(rs.getLong("lastRecTime"));
        gourdChildUserData.setPassId(rs.getInt("passId"));
        gourdChildUserData.setGridList(JSON.parseObject(rs.getString("gridList"), new TypeReference<List<Grid>>() {}));
        gourdChildUserData.setNextGrid(JSON.parseObject(rs.getString("nextGrid"), new TypeReference<Grid>(){}));
        gourdChildUserData.setPassStar(JSON.parseObject(rs.getString("passStar"), new TypeReference<java.util.Map<Integer, Integer>>() {}));
        gourdChildUserData.setLotusLv(rs.getInt("lotusLv"));
        gourdChildUserData.setTrainDamage(rs.getLong("trainDamage"));
        gourdChildUserData.setNormalDamage(rs.getLong("normalDamage"));
        gourdChildUserData.setPassDetail(JSON.parseObject(rs.getString("passDetail"), new TypeReference<GourdChildPassDetail>(){}));
        gourdChildUserData.setTotalTimes(rs.getInt("totalTimes"));
        gourdChildUserData.setCommonTimes(rs.getInt("commonTimes"));
        gourdChildUserData.setDrawBoxList(JSON.parseObject(rs.getString("drawBoxList"), new TypeReference<java.util.List<java.lang.Integer>>() {}));
        gourdChildUserData.setGetSharedBox(rs.getBoolean("getSharedBox"));
        gourdChildUserData.setHistoryPassId(rs.getInt("historyPassId"));
        return gourdChildUserData;
    }
}
